package job.manager.client.mb;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import job.manager.db.ec.UserGroupsEB;
import job.manager.db.ec.UsersEB;
import job.manager.db.sb.UserRemote;

/**
 *  The user ManagedBean is responsible for connection to the Enterprise Beans
 *  and creating new Users.
 */
public class UserMB {

    @EJB
    private UserRemote userBean;
    private UsersEB user;
    private String userName;
    private String password;
    private String email;
    private boolean isAdmin;
    private Integer profileId;
    private UserGroupsEB userGroup;
    private String msg;

    /** Creates a new instance of UserMB */
    public UserMB() {
        this.onInit();
    }

    @PostConstruct
    public void onInit() {
        this.resetProperties();
    }

    private void resetProperties() {
        this.user = null;
        this.msg = "";
        this.userName = "";
        this.password = "";
        this.email = "";
        this.isAdmin = false;
        this.profileId = 0;
        this.userGroup = null;

    }

    //Action to create a new user
    public void insertUser(ActionEvent arg) {
        this.problemSolver();
        this.setValues();
        try {
            this.userBean.createUser(this.user);
            this.Clear(arg);
            this.msg = "Användare tillagd";
        } catch (Exception e) {
            this.msg = e.getMessage();
        }
    }

    //set values to a entity class
    public void setValues() {
        if (this.user == null) {
            this.user = new UsersEB();
        }
        this.user.setUserName(this.userName);
        this.user.setPassword(this.password);
        this.user.setEmail(this.email);
        this.user.setUsergroup(this.userGroup);
        this.user.setIsAdmin(this.isAdmin);
        if (this.profileId > 0) {
            this.user.setProfileId(this.profileId);
        }

        // set these values just to get the validation
        this.setUserName(this.user.getUserName());
        this.setPassword(this.user.getPassword());
        this.setEmail(this.user.getEmail());
    }

    //clear the input fields
    public void Clear(ActionEvent arg) {
        this.onInit();
    }

    //Spring security bug fix
    private void problemSolver() {
        if (this.userBean == null) {
            this.userBean = ConnectFactory.getUserBean();
        }
    }

    //Set and get methods
    public void setUserName(String username) {
        this.userName = username;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return this.password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return this.email;
    }

    public void setIsAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    public boolean getIsAdmin() {
        return this.isAdmin;
    }

    public void setProfileId(Integer id) {
        this.profileId = id;
    }

    public Integer getProfileId() {
        return this.profileId;
    }

    public String getUserGroupId() {
        if (this.userGroup == null) {
            return "";
        }
        return this.userGroup.getId().toString();
    }

    public void setUserGroupId(String userGroupId) {
        int groupId = Integer.parseInt(userGroupId);
        this.userGroup = userBean.getUserGroup(groupId);
    }

    public String getMsg() {
        return msg;
    }
}
